<?php
include_once 'data.php';
include_once 'functions.php';

if (isset($_GET['file']) && isset($_SESSION['auth'])) {

	database_connect($database_path, 'library');
	
	$user_query = $dbHandle->quote($_SESSION['user_id']);
	$file_query = $dbHandle->quote($_GET['file']);
	
	$result = $dbHandle->query("SELECT rowid FROM shelves WHERE userID=$user_query AND fileID=$file_query LIMIT 1");
	$relation = $result->fetchColumn();
	$result = null;

	if (!$relation) {
		$update = $dbHandle->exec("INSERT OR IGNORE INTO shelves (userID,fileID) VALUES ($user_query,$file_query)");
		if($update) echo 'added';
	} else {
		$update = $dbHandle->exec("DELETE FROM shelves WHERE rowid=$relation");
              if (isset($_GET['selection']) && $_GET['selection'] == 'shelf') {
			unset($_SESSION['export_files'][array_search($_GET['file'], $_SESSION['export_files'])]);
			$_SESSION['export_files'] = array_values($_SESSION['export_files']);
		}
		if($update) echo 'removed';
	}

	$dbHandle = null;
}
?>